home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / __MANDEL / MANDELBR / CMANDELD.C1 < prev    next >
Text File  |  1992-03-25  |  2KB  |  142 lines

  1. //    CMandelDataFile.c
  2.  
  3. #include "CMandelDataFile.h"
  4. #include "CMandelDoc.h"
  5.  
  6. #define MIN_RUN        3
  7.  
  8. void
  9. CMandelDataFile::IMandelDataFile(CMandelDoc *theMandelDoc)
  10. {
  11.     CDataFile::IDataFile();
  12.     
  13.     itsMandelDoc = theMandelDoc;
  14. }
  15.  
  16. void
  17. CMandelDataFile::WriteAll(Handle theDataH)
  18. {
  19.     TMandelInfo    aInfoRec;
  20.     TDwellH        aDwellH;
  21.     TDwell        n, *p, *q;
  22.     
  23.     itsMandelDoc->GetMandelInfo(&aInfoRec);
  24.     WriteSome((Ptr)&aInfoRec, sizeof(aInfoRec));
  25.  
  26. /*    run-length encoding...
  27.     a run can be either two-word unit where word 0 is a count and word 1 is
  28.     the value to be repeated for count, or a n-word unit where word 0 is a
  29.     count and the following n-1 words are taken literally.
  30.     the first run is of the former.
  31.     */
  32.     
  33.     aDwellH = itsMandelDoc->GetDwellsHandle();
  34.     HLock(aDwellH);
  35.     
  36.     p = *aDwellH;
  37.     q = p + GetHandleSize(aDwellH) / sizeof(TDwell);
  38.     
  39.     while (p < q)
  40.     {
  41.         n = cGetRunSize(p, q);
  42.         if (n >= MIN_RUN)
  43.         {
  44.             TDwell    aRun[2];
  45.             aRun[0] = n;
  46.             aRun[1] = *p;
  47.             WriteSome((Ptr)aRun, sizeof(aRun));
  48.             p += n;
  49.         }
  50.         else
  51.         {
  52.             n = 0;
  53.             WriteSome((Ptr)&n, sizeof(n));
  54.         }
  55.         
  56.         n = cGetNurSize(p, q);
  57.         WriteSome((Ptr)&n, sizeof(n));
  58.         WriteSome((Ptr)p, sizeof(*p) * n);
  59.         
  60.         p += n;
  61.     }
  62.     
  63.     HUnlock(aDwellH);
  64.     
  65.     n = 0;
  66.     WriteSome((Ptr)&n, sizeof(n));
  67.     WriteSome((Ptr)&n, sizeof(n));
  68.     WriteSome((Ptr)&n, sizeof(n));
  69. }
  70.  
  71. Handle    CMandelDataFile::ReadAll(void)
  72. {
  73.     TMandelInfo        aInfoRec;
  74.     TDwellH            aDwellsH;
  75.     TDwell            aDwell, i, n, *p, *q;
  76.     
  77.     ReadSome((Ptr)&aInfoRec, sizeof(aInfoRec));
  78.     itsMandelDoc->SetMandelInfo(&aInfoRec);
  79.  
  80.     aDwellsH = itsMandelDoc->GetDwellsHandle();
  81.     HLock(aDwellsH);
  82.     
  83.     p = *aDwellsH;
  84.     q = p + GetHandleSize(aDwellsH) / sizeof(TDwell);
  85.     
  86.     while (p < q)
  87.     {
  88.         ReadSome((Ptr)&n, sizeof(n));
  89.         if (n)
  90.             ReadSome((Ptr)&aDwell, sizeof(aDwell));
  91.  
  92.         for (i = 0; i < n; i++)
  93.             *p++ = aDwell;
  94.         
  95.         ReadSome((Ptr)&n, sizeof(n));
  96.         ReadSome((Ptr)p, sizeof(*p) * n);
  97.         p += n;
  98.     }
  99.     
  100.     HUnlock(aDwellsH);
  101.     
  102.     itsMandelDoc->DoDraw();
  103.     return NULL;
  104. }
  105.  
  106.  
  107. unsigned
  108. CMandelDataFile::cGetRunSize(unsigned *p, unsigned *q)
  109. {
  110.     unsigned long    i, n;
  111.     
  112.     n = q - p;
  113.     if (n > 65535)
  114.         n = 65535;
  115.     
  116.     for (i = 1; i < n; i++)
  117.     {
  118.         if (p[i] != *p)
  119.             break;
  120.     }
  121.     
  122.     return i;
  123. }
  124.  
  125. unsigned
  126. CMandelDataFile::cGetNurSize(unsigned *p, unsigned *q)
  127. {
  128.     unsigned long    i, n;
  129.     
  130.     n = q - p;
  131.     if (n > 65535)
  132.         n = 65535;
  133.     
  134.     for (i = 0; i < n; i++)
  135.     {
  136.         if (cGetRunSize(p + i, q) >= MIN_RUN)
  137.             break;
  138.     }
  139.     
  140.     return i;
  141. }
  142.